package com.yyplatform.admin.platform.config.service.impl;

import java.util.List;
import java.util.Set;

import com.yyplatform.admin.platform.config.repository.SysDictItemRepository;

import org.modelmapper.ModelMapper;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.stereotype.Service;

import com.yyplatform.admin.platform.config.model.dto.SysDictDTO;
import com.yyplatform.admin.platform.config.model.dto.SysDictFormDTO;
import com.yyplatform.admin.platform.config.model.dto.SysDictQueryDTO;
import com.yyplatform.admin.platform.config.model.entity.SysDictEntity;
import com.yyplatform.admin.platform.config.repository.SysDictRepository;
import com.yyplatform.admin.platform.config.service.SysDictService;
import com.yyplatform.admin.infra.exception.ErrorCode;
import com.yyplatform.admin.infra.exception.ServiceException;
import com.yyplatform.admin.infra.utils.DateUtils;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.springframework.transaction.annotation.Transactional;

/**
 * Created on 2024-09-14
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysDictServiceImpl implements SysDictService {
    private SysDictRepository sysDictRepository;
    private SysDictItemRepository sysDictItemRepository;
    private ModelMapper modelMapper;


    @Override
    public Page<SysDictDTO> query(SysDictQueryDTO pageQueryDTO) {

        Page<SysDictEntity> entityPage =
                sysDictRepository.findPage(pageQueryDTO.getName(), pageQueryDTO.getCode(),
                        pageQueryDTO.getEnabled(),
                        DateUtils.getBeginDate(pageQueryDTO.getCreateTimeArr()),
                        DateUtils.getEndDate(pageQueryDTO.getCreateTimeArr()),
                        pageQueryDTO.getPageRequestOrderByIdDesc());

        List<SysDictDTO> list = entityPage.getContent().stream()
                .map(e -> modelMapper.map(e, SysDictDTO.class))
                .toList();

        return new PageImpl<>(list, entityPage.getPageable(), entityPage.getTotalElements());
    }


    @Override
    public SysDictDTO get(Long id) {
        SysDictEntity entity = sysDictRepository.getReferenceById(id);
        return modelMapper.map(entity, SysDictDTO.class);
    }

    @Override
    public void save(SysDictFormDTO sysDictFormDTO) {

        SysDictEntity checkEntity = sysDictRepository.findByCode(sysDictFormDTO.getCode());

        if (checkEntity != null) {
            throw ServiceException.of(ErrorCode.DB_KEY_DUMP);
        }

        SysDictEntity toEntity = modelMapper.map(sysDictFormDTO, SysDictEntity.class);
        sysDictRepository.save(toEntity);
    }

    @Override
    public void update(SysDictFormDTO formDTO) {
        SysDictEntity oldEntity = sysDictRepository.getReferenceById(formDTO.getId());

        oldEntity.setEnabled(formDTO.getEnabled());
        oldEntity.setName(formDTO.getName());
        oldEntity.setRemark(formDTO.getRemark());

        sysDictRepository.save(oldEntity);
    }


    @Transactional
    @Override
    public void delete(Long id) {
        sysDictRepository.deleteById(id);
        sysDictItemRepository.deleteByDictId(id);
    }

    @Override
    public void delete(Set<Long> idSet) {
        //sysDictRepository.deleteAllById(idSet);
    }

}
